/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main.Weighting;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import main.KBProcess.Element;
import main.KBProcess.Fragment;
import main.NER.Trie.NER_Candidate;

/**
 *
 * @author Isaac Osesina
 */
public class BayesianWeightingFunction extends AbstractWeightingFunction implements WeightingMethod, Serializable{

    List<Element> entityList;

    public BayesianWeightingFunction(List<Element> entityList) {
        this.entityList = entityList;
    }

    
    @Override
	public WeightingScore getEvaluateScore(NER_Candidate candidate, double intrinsicThreshold, 
                                           double lCxtThreshold, double rCxtThreshold) {

        List<Fragment> intersectFragments = intersectFragments(candidate);
        Pattern entityClassRegex = candidate.getEntityClassRegex();
        //some of the entities are not of the same type as candidate <==> ambiguous contexts
        Set<Element> entities_intrinsic_not_null = entitiesIntrinsicProbabilityNotNull(entityClassRegex, intersectFragments, intrinsicThreshold);
        //filter entities of the same type as candidate
        Set<Element> entities_intrinsic_not_null_same_type = entityTypeSubset_ID(entityList, entities_intrinsic_not_null, candidate);

        double lCxtProb = getContextProbability(candidate, true, lCxtThreshold);
        double rCxtProb = getContextProbability(candidate, false, rCxtThreshold);
        double bayesianProb = entities_intrinsic_not_null.size()>0 ?
                              (double)entities_intrinsic_not_null_same_type.size()/entities_intrinsic_not_null.size():entities_intrinsic_not_null.size();

        double contextualProb = lCxtProb * rCxtProb;
        double intrinsicProb = getIntrinsicProbability(candidate.getEntityValue(), entityClassRegex, intrinsicThreshold);
        WeightingScore weightingScore = new WeightingScore(contextualProb, lCxtProb, rCxtProb, intrinsicProb, bayesianProb, this.getClass().getName());
        weightingScore.setThreshold(intrinsicThreshold, lCxtThreshold, rCxtThreshold);
        return weightingScore;
    }
}
